﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Builderator.Core;
using Builderator.Firefly.Windows;
using Builderator.Core.Components;

namespace Builderator.Firefly.Controls
{
    /// <summary>
    /// Interaction logic for BuilderatorSettingsControl.xaml
    /// </summary>
    public partial class BuilderatorSettingsControl : UserControl
    {
        public static readonly DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName", typeof(String), typeof(BuilderatorSettingsControl));
        public static readonly DependencyProperty PluginsProperty = DependencyProperty.Register("Plugins", typeof(String), typeof(BuilderatorSettingsControl));
        public static readonly DependencyProperty ServersProperty = DependencyProperty.Register("Servers", typeof(ServerList), typeof(BuilderatorSettingsControl));

        public BuilderatorSettings settings;

        public String ServerName
        {
            get
            {
                return (String)this.GetValue(ServerNameProperty);
            }
            set
            {
                this.settings.Name = value;

                this.SetValue(ServerNameProperty, value);
            }
        }

        public String Plugins
        {
            get
            {
                return (String)this.GetValue(PluginsProperty);
            }
            set
            {
                this.settings.Plugins = value;

                this.SetValue(PluginsProperty, value);
            }
        }

        public ServerList Servers
        {
            get
            {
                return (ServerList)this.GetValue(ServersProperty);
            }
            set
            {
                this.settings.Servers = value;

                this.SetValue(ServersProperty, value);
            }
        }

        private BuilderatorSettingsControl()
        {
            InitializeComponent();
        }

        public BuilderatorSettingsControl(BuilderatorSettings settings)
            : this()
        {
            this.DataContext = this;

            this.settings = settings;

            this.ServerName = this.settings.Name;

            this.Plugins = this.settings.Plugins;

            this.Servers = this.settings.Servers;
        }

        private void pluginBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Plugins = dialog.SelectedPath;
            }
        }   

        private void serverAddButton_Click(object sender, RoutedEventArgs e)
        {
            DomainBrowserWindow window = new DomainBrowserWindow();

            if (window.ShowDialog() == true)
            {
                this.Servers.Add(window.Selected);
            }
        }

        private void serverRemoveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.serverListBox.SelectedIndex < 0)
                return;

            this.Servers.Remove((String)this.serverListBox.SelectedItem);
        }
    }
}